package com.devbobcorn.nekorization.client.rendering;

import java.util.Random;

import com.devbobcorn.nekorization.Nekorization;
import com.devbobcorn.nekorization.client.model.NekoModel;
import com.devbobcorn.nekorization.entities.NekoEntity;
import com.mojang.blaze3d.vertex.PoseStack;

import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.entity.RenderLayerParent;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

@OnlyIn(Dist.CLIENT)
public class NekoInnerLayer extends NekoAbstractLayer {
	private static final int BLINK_DURATION = 60; // 3 seconds, 60 ticks

    private static final ResourceLocation SHIRT = new ResourceLocation(Nekorization.MODID, "textures/entity/neko/shirt_inner.png");
	private static final ResourceLocation SKIRT = new ResourceLocation(Nekorization.MODID, "textures/entity/neko/skirt_inner.png");
	private static final ResourceLocation SHOES = new ResourceLocation(Nekorization.MODID, "textures/entity/neko/shoes_inner.png");

	public NekoInnerLayer(RenderLayerParent<NekoEntity, NekoModel<NekoEntity>> parent, NekoModel<NekoEntity> model) {
		super(parent, model);
	}

	public void render(PoseStack stack, MultiBufferSource buffers, int l, NekoEntity entity, float a, float b, float c, float d, float e, float f) {
		// Setup model, then render hair...
		setHairVisible();
		coloredCutoutModelCopyLayerRender(this.getParentModel(), this.nekoModel, entity.getInnerHair(), stack, buffers, l, entity, a, b, d, e, f, c, 1.0F, 1.0F, 1.0F);
		if (entity.isInvisible()) return; // Invisibility check...
		// Render blinkin' eyes...
		Random rand = new Random(entity.getId()); // Add a random factor to avoid Nekoz blink synchronously...
		long ticks = entity.level.getGameTime() + rand.nextInt(BLINK_DURATION);
		int blinkTicks = (int)(ticks % BLINK_DURATION);
		if (blinkTicks >= 5)
			renderColoredCutoutModel(this.nekoModel, entity.getEyes()[0], stack, buffers, l, entity, 1.0F, 1.0F, 1.0F);
		else { // 0 - 4
			renderColoredCutoutModel(this.nekoModel, entity.getEyes()[2 - Math.abs(blinkTicks - 2)], stack, buffers, l, entity, 1.0F, 1.0F, 1.0F);
		}
		// Render shirt...
		setShirtVisible();
		renderColoredCutoutModel(this.nekoModel, SHIRT, stack, buffers, l, entity, 1.0F, 1.0F, 1.0F);
		// Render skirt...
		setSkirtVisible();
		renderColoredCutoutModel(this.nekoModel, SKIRT, stack, buffers, l, entity, 1.0F, 1.0F, 1.0F);
		// Render shoes...
		this.nekoModel.body.visible = false;
		renderColoredCutoutModel(this.nekoModel, SHOES, stack, buffers, l, entity, 1.0F, 1.0F, 1.0F);
	}
}